package com.tj.retrofit_notes;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2016/10/14 0014.
 */
public class HttpMethod {

    public static String BASE_URL = "http://gank.io/api/";
    public static final int DEFAULT_TIMEOUT = 15;
    private GankApi gankApi;

    private volatile static HttpMethod mInstance;

    private HttpMethod(){
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        //这里的log有4个level， NONE，BASIC，HEADERS，BODY
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)//打印log
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(BASE_URL)//以“/”结尾
                .addConverterFactory(GsonConverterFactory.create())//自动转换json为对象
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        gankApi = retrofit.create(GankApi.class);
    }

    public static HttpMethod getInstance(){
        if(mInstance == null){
            synchronized (HttpMethod.class){
                if(mInstance == null){
                    mInstance = new HttpMethod();
                }
            }
        }
        return mInstance;
    }

    public GankApi getGankApi(){
        return gankApi;
    }
}
